package org.gitman.agentplatform.ui.graph;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;

import org.gitman.agentplatform.core.PlatformManager;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

public class GraphMouseAdapter extends MouseAdapter{
	
	private GraphDisplPanel graphDisplPanel;
	private GraphEditor graphEditor;
	private PlatformManager platformManager;
	
	public GraphMouseAdapter(GraphDisplPanel graphDisplPanel){
		this.graphDisplPanel = graphDisplPanel;
		this.platformManager = PlatformManager.getInstatnce();		
		graphEditor = GraphEditor.getInstance();
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		mxGraph graph = graphEditor.getGraph();
		mxGraphComponent graphComponent = graphEditor.getGraphComponent();
		if(graphEditor.isEdited()){
			if(arg0.getClickCount() == 2 && arg0.getButton() == MouseEvent.BUTTON1 ){
				graphDisplPanel.insertVertex(arg0.getX()-graphDisplPanel.getDotSize()/2, arg0.getY()-graphDisplPanel.getDotSize()/2);
			}else if (arg0.getButton() == MouseEvent.BUTTON3){
				graph.getModel().beginUpdate();
				try{
					graph.removeCells(graph.getSelectionCells());
				}finally{
					graph.getModel().endUpdate();
				}
			}
		}else{
			/*AddedAgentInfo info = platformManager.getSimulation().getAgentManager().getAddedAgentInfo();
			if(info != null && info.canSelectPoints()){
				mxCell point = (mxCell) graph.getSelectionCell();
				if(point != null && point.isVertex()){					
					graphComponent.refresh();
					if(arg0.getClickCount() == 2 && arg0.getButton() == MouseEvent.BUTTON1 ){	
						info.setStartPoint(point);
					}else if (arg0.getButton() == MouseEvent.BUTTON3){	
						info.setEndPoint(point);
					}
				}
			}*/
		}
	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		int rotation = e.getWheelRotation();
		graphDisplPanel.changeScale(-rotation);		
	}
}
